﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Windows.Media;
using System.Reflection;
using System.Timers;

namespace SimpleBomberman
{
    public partial class MainForm : Form
    {
        private MediaPlayer mediaPlayer = new MediaPlayer();
        private MediaPlayer mediaPlayer2 = new MediaPlayer();
        //public int StateForPlayer = 0;
        private Bitmap bitmap;
        private Graphics graphics;
        private Game game;
        private InitialForm form2;
        private ResultsForm form3;
        
        public MainForm()
        {
            InitializeComponent();
            bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
            pictureBox1.Image = bitmap;
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(TimerEventProcessor);
        }

        private void Media_Ended(object sender, EventArgs e)
        {
            if (game.Bomberman.DeadFlag != true && game.Bomberman.IsFinish != true)
            {
                mediaPlayer.Position = TimeSpan.Zero;
                mediaPlayer.Play();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            game = new Game();
            game.ReadLevels();
            this.StartPosition = FormStartPosition.CenterScreen;
            form2 = new InitialForm(game.LevelList.Count);
            form2.ShowDialog();
            
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //--- Start ---
            if (e.KeyData == Keys.S)
            {
                game.Start(ref game, ref graphics, ref bitmap, pictureBox1, form2);
                label3.Text = "0";
                label5.Text = "0";

                string direction = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string FullFileName = Path.Combine(direction, "MainTheme.mp3");
                mediaPlayer.Open(new Uri(FullFileName));
                mediaPlayer.Volume = 1.0;
                mediaPlayer.Play();
                mediaPlayer.MediaEnded += Media_Ended;

                label1.Text = "";
                timer1.Enabled = true;
            }

            if (!game.Bomberman.IsFinish && (timer1.Enabled == true))
            {
                //--- Move down ---
                if (e.KeyData == Keys.Down)
                {
                    if (game.CanBombermanMove(e))
                    {
                        game.Bomberman.DownFlag = true;
                        graphics = Graphics.FromImage(bitmap);
                        graphics.FillRectangle(System.Drawing.Brushes.Green, game.Bomberman.X, game.Bomberman.Y, game.Bomberman.Length, game.Bomberman.Length);
                        game.MoveBomberman();
                        game.Bomberman.Draw(graphics);

                        game.ReDrawBomb(graphics);

                        //--- Bonus ---
                        game.CheckBonusFinding(label3);

                        //--- ExitDoor ---
                        game.CheckExitDoorFinding();

                        pictureBox1.Image = bitmap;
                        game.Bomberman.DownFlag = false;
                        game.CheckDefeat(this.timer1, this.pictureBox1, this.mediaPlayer);
                        if (game.Bomberman.IsFinish)
                        {
                            game.CheckWin(this.timer1, this.mediaPlayer);
                            form3 = new ResultsForm(game.LevelList[game.LevelIndex].Time, game.LevelList[game.LevelIndex].Points,
                                game.LevelIndex);
                            label1.Text = "Press \'S\' to start";
                            form2.Visible = true;
                            form3.Show();
                        }
                        if (game.Bomberman.DeadFlag == true)
                        {
                            label1.Text = "Press \'S\' to start";
                            form2.ShowDialog();
                        }
                    }
                }
                //--- Move up ---
                if (e.KeyData == Keys.Up)
                {
                    if (game.CanBombermanMove(e))
                    {
                        game.Bomberman.UpFlag = true;
                        graphics = Graphics.FromImage(bitmap);
                        graphics.FillRectangle(System.Drawing.Brushes.Green, game.Bomberman.X, game.Bomberman.Y, game.Bomberman.Length, game.Bomberman.Length);
                        game.MoveBomberman();
                        game.Bomberman.Draw(graphics);

                        game.ReDrawBomb(graphics);

                        //--- Bonus ---
                        game.CheckBonusFinding(label3);

                        //--- ExitDoor ---
                        game.CheckExitDoorFinding();

                        pictureBox1.Image = bitmap;
                        game.Bomberman.UpFlag = false;
                        game.CheckDefeat(this.timer1, this.pictureBox1, this.mediaPlayer);
                        if (game.Bomberman.IsFinish)
                        {
                            game.CheckWin(this.timer1, this.mediaPlayer);
                            form3 = new ResultsForm(game.LevelList[game.LevelIndex].Time, game.LevelList[game.LevelIndex].Points,
                                game.LevelIndex);
                            label1.Text = "Press \'S\' to start";
                            form2.Visible = true;
                            form3.Show();
                        }
                        if (game.Bomberman.DeadFlag == true)
                        {
                            label1.Text = "Press \'S\' to start";
                            form2.ShowDialog();
                        }
                    }
                }
                //--- Move left ---
                if (e.KeyData == Keys.Left)
                {
                    if (game.CanBombermanMove(e))
                    {
                        game.Bomberman.LeftFlag = true;
                        graphics = Graphics.FromImage(bitmap);
                        graphics.FillRectangle(System.Drawing.Brushes.Green, game.Bomberman.X, game.Bomberman.Y, game.Bomberman.Length, game.Bomberman.Length);
                        game.MoveBomberman();
                        game.Bomberman.Draw(graphics);

                        game.ReDrawBomb(graphics);

                        //--- Bonus ---
                        game.CheckBonusFinding(label3);

                        //--- ExitDoor ---
                        game.CheckExitDoorFinding();

                        pictureBox1.Image = bitmap;
                        game.Bomberman.LeftFlag = false;
                        game.CheckDefeat(this.timer1, this.pictureBox1, this.mediaPlayer);
                        if (game.Bomberman.IsFinish)
                        {
                            game.CheckWin(this.timer1, this.mediaPlayer);
                            form3 = new ResultsForm(game.LevelList[game.LevelIndex].Time, game.LevelList[game.LevelIndex].Points,
                                game.LevelIndex);
                            label1.Text = "Press \'S\' to start";
                            form2.Visible = true;
                            form3.Show();
                        }
                        if (game.Bomberman.DeadFlag == true)
                        {
                            label1.Text = "Press \'S\' to start";
                            form2.ShowDialog();
                        }
                    }
                }
                //--- Move right ---
                if (e.KeyData == Keys.Right)
                {
                    if (game.CanBombermanMove(e))
                    {
                        game.Bomberman.RightFlag = true;
                        graphics = Graphics.FromImage(bitmap);
                        graphics.FillRectangle(System.Drawing.Brushes.Green, game.Bomberman.X, game.Bomberman.Y, game.Bomberman.Length, game.Bomberman.Length);
                        game.MoveBomberman();
                        game.Bomberman.Draw(graphics);

                        game.ReDrawBomb(graphics);

                        //--- Bonus ---
                        game.CheckBonusFinding(label3);

                        //--- ExitDoor ---
                        game.CheckExitDoorFinding();

                        pictureBox1.Image = bitmap;
                        game.Bomberman.RightFlag = false;
                        game.CheckDefeat(this.timer1, this.pictureBox1, this.mediaPlayer);
                        if (game.Bomberman.IsFinish)
                        {
                            game.CheckWin(this.timer1, this.mediaPlayer);
                            form3 = new ResultsForm(game.LevelList[game.LevelIndex].Time, game.LevelList[game.LevelIndex].Points,
                                game.LevelIndex);
                            label1.Text = "Press \'S\' to start";
                            form2.Visible = true;
                            form3.Show();
                        }
                        if (game.Bomberman.DeadFlag == true)
                        {
                            label1.Text = "Press \'S\' to start";
                            form2.ShowDialog();
                        }
                    }
                }
                //--- Put the bomb ---
                if (e.KeyData == Keys.B)
                {
                    if (game.Bomb.X == 0 && game.Bomb.Y == 0)
                    {
                        graphics = Graphics.FromImage(bitmap);
                        game.Bomb.X = game.Bomberman.X;
                        game.Bomb.Y = game.Bomberman.Y;
                        game.Bomb.Draw(graphics);
                        pictureBox1.Image = bitmap;
                    }
                }
            }
        }

        private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
        {
            graphics = Graphics.FromImage(bitmap);

            game.Explosion(graphics, mediaPlayer2, label3);

            for (int i = 0; i < game.LevelList[game.LevelIndex].EnemyList.Count; i++)
            {
                graphics.FillRectangle(System.Drawing.Brushes.Green, game.LevelList[game.LevelIndex].EnemyList[i].X,
                    game.LevelList[game.LevelIndex].EnemyList[i].Y, game.LevelList[game.LevelIndex].EnemyList[i].Length,
                    game.LevelList[game.LevelIndex].EnemyList[i].Length);
            }
            for (int i = 0; i < game.LevelList[game.LevelIndex].EnemyList.Count; i++)
            {
                game.ChooseDirectionAndMoveEnemy(game.LevelList[game.LevelIndex].EnemyList[i]);
            }
            for (int i = 0; i < game.LevelList[game.LevelIndex].EnemyList.Count; i++)
            {
                game.LevelList[game.LevelIndex].EnemyList[i].Draw(graphics);
            }
            pictureBox1.Image = bitmap;

            game.CheckDefeat(this.timer1, this.pictureBox1, this.mediaPlayer);

            if (game.Bomberman.DeadFlag == true)
            {
                //form2.ShowDialog();
                if (!form2.IsDisposed)
                {
                    form2.ShowDialog();
                }
                else
                {
                    form2 = new InitialForm(game.LevelList.Count);
                    form2.ShowDialog();
                }
            }

            game.LevelList[game.LevelIndex].Time += 1;
            label5.Text = game.LevelList[game.LevelIndex].Time.ToString();
            
        }
        
    }
}
